package com.jt.www.util;

import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;

/**
 * @author
 */
public class UserAgentUtils {

    /**
     * 获取用户代理对象
     *
     * @param request
     * @return
     */
    public static UserAgent getUserAgent(HttpServletRequest request) {
        return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
    }

    /**
     * 获取设备类型
     *
     * @param request
     * @return
     */
    public static DeviceType getDeviceType(HttpServletRequest request) {
        return getUserAgent(request).getOperatingSystem().getDeviceType();
    }

    /**
     * 是否是PC
     *
     * @param request
     * @return
     */
    public static boolean isComputer(HttpServletRequest request) {
        return DeviceType.COMPUTER.equals(getDeviceType(request));
    }

    /**
     * 是否是手机
     *
     * @param request
     * @return
     */
    public static boolean isMobile(HttpServletRequest request) {
        return DeviceType.MOBILE.equals(getDeviceType(request));
    }

    /**
     * 是否是平板
     *
     * @param request
     * @return
     */
    public static boolean isTablet(HttpServletRequest request) {
        return DeviceType.TABLET.equals(getDeviceType(request));
    }

    /**
     * 是否是手机和平板
     *
     * @param request
     * @return
     */
    public static boolean isMobileOrTablet(HttpServletRequest request) {
        DeviceType deviceType = getDeviceType(request);
        return DeviceType.MOBILE.equals(deviceType) || DeviceType.TABLET.equals(deviceType);
    }

    /**
     * 是否Android
     *
     * @param request
     */
    public static boolean isAndroid(HttpServletRequest request) {
        String userAgent = request.getHeader("user-agent");
        boolean status = userAgent.contains("Android");
        if (status == true) {
            System.out.println("Android终端访问~");
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否是iPhone
     *
     * @param request
     * @return
     */
    public static boolean isIOS(HttpServletRequest request) {
        String userAgent = request.getHeader("user-agent");
        boolean status = userAgent.contains("iPhone");
        if (status == true) {
            System.out.println("IOS终端访问~");
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是什么客户端
     *
     * @param request
     * @return
     */
    public static int judgeClient(HttpServletRequest request) {
        int sourceType = 0;
        if (UserAgentUtils.isComputer(request)) {
            sourceType = 1;
        } else if (UserAgentUtils.isMobile(request)) {
            boolean android = UserAgentUtils.isAndroid(request);
            if (UserAgentUtils.isAndroid(request)) {
                sourceType = 2;
            } else if (UserAgentUtils.isIOS(request)) {
                sourceType = 3;
            }
        }
        return sourceType;
    }
}